package com.zhaoxi.project.md.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.util.StringUtil;
import com.zhaoxi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhaoxi.project.md.mapper.MdGoodsAttrValueMapper;
import com.zhaoxi.project.md.domain.MdGoodsAttrValue;

/**
 * 商品下面的属性值Service业务层处理
 *
 * @author zhaoxi
 * @date 2023-08-28
 */
@Service
public class MdGoodsAttrValueServiceImpl
{
    @Autowired
    private MdGoodsAttrValueMapper mdGoodsAttrValueMapper;

    /**
     * 查询商品下面的属性值
     *
     * @param id 商品下面的属性值主键
     * @return 商品下面的属性值
     */

    public MdGoodsAttrValue selectMdGoodsAttrValueById(Long id)
    {
        return mdGoodsAttrValueMapper.selectMdGoodsAttrValueById(id);
    }

    public JSONArray getValueArrById(Long id) {
        JSONArray valueArr = new JSONArray();
        MdGoodsAttrValue ma = selectMdGoodsAttrValueById(id);
        if(ma!=null) {
            String value = ma.getAttributeValue();
            if(StringUtil.isNotEmpty(value)){
                String[] arr = value.split("\\|");
                for(String v: arr) {
                    JSONObject item = new JSONObject();
                    item.put("value",v);
                    item.put("name",v);
                    valueArr.add(item);
                }
            }
        }
        return valueArr;
    }

    /**
     * 查询商品下面的属性值列表
     *
     * @param mdGoodsAttrValue 商品下面的属性值
     * @return 商品下面的属性值
     */

    public List<MdGoodsAttrValue> selectMdGoodsAttrValueList(MdGoodsAttrValue mdGoodsAttrValue)
    {
        return mdGoodsAttrValueMapper.selectMdGoodsAttrValueList(mdGoodsAttrValue);
    }

    /**
     * 新增商品下面的属性值
     *
     * @param mdGoodsAttrValue 商品下面的属性值
     * @return 结果
     */

    public int insertMdGoodsAttrValue(MdGoodsAttrValue mdGoodsAttrValue)
    {
        mdGoodsAttrValue.setCreateTime(DateUtils.getNowDate());
        return mdGoodsAttrValueMapper.insertMdGoodsAttrValue(mdGoodsAttrValue);
    }

    /**
     * 修改商品下面的属性值
     *
     * @param mdGoodsAttrValue 商品下面的属性值
     * @return 结果
     */

    public int updateMdGoodsAttrValue(MdGoodsAttrValue mdGoodsAttrValue)
    {
        mdGoodsAttrValue.setUpdateTime(DateUtils.getNowDate());
        return mdGoodsAttrValueMapper.updateMdGoodsAttrValue(mdGoodsAttrValue);
    }

    /**
     * 批量删除商品下面的属性值
     *
     * @param ids 需要删除的商品下面的属性值主键
     * @return 结果
     */

    public int deleteMdGoodsAttrValueByIds(Long[] ids)
    {
        return mdGoodsAttrValueMapper.deleteMdGoodsAttrValueByIds(ids);
    }

    /**
     * 删除商品下面的属性值信息
     *
     * @param id 商品下面的属性值主键
     * @return 结果
     */

    public int deleteMdGoodsAttrValueById(Long id)
    {
        return mdGoodsAttrValueMapper.deleteMdGoodsAttrValueById(id);
    }



//    private MdGoodsAttrValue getInfoById(Long id) {
////        MaterialAttributeExample example = new MaterialAttributeExample();
////        example.createCriteria().andIdEqualTo(id).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
////        List<MaterialAttribute> list = materialAttributeMapper.selectByExample(example);
////        if(list!=null && list.size()>0) {
////            return list.get(0);
////        } else {
////            return null;
////        }
//
//        mdGoodsAttrValueMapper.selectMdGoodsAttrValueById(id);
//        return null;
//    }
}
